﻿using System.Collections;
using Fluffy.Library.Comparers;
using System.ComponentModel;

namespace Fluffy.Archive.Comparers
{
	internal abstract class ArchiveContentComparerBase : IComparerWithDirection
	{
		public ListSortDirection Direction { get; set; }

		public int Compare(object x, object y)
		{
			IArchiveEntity first = (IArchiveEntity) x;
			IArchiveEntity second = (IArchiveEntity) y;

			if (first is RootFolderArchiveEntityViewModel)
				return -1;
			if (second is RootFolderArchiveEntityViewModel)
				return 1;

			if (first.IsDirectory && !second.IsDirectory)
				return -1;
			if (!first.IsDirectory && second.IsDirectory)
				return 1;

			int value = Comparer.Default.Compare(GetValue(first), GetValue(second));
			return Direction == ListSortDirection.Ascending ? value : -value;
		}

		protected abstract object GetValue(IArchiveEntity entity);
	}
}
